package com.jcfk.eam.service.base;

import cn.hutool.core.collection.ListUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamTeamMemberAbilityDao;
import com.jcfk.eam.domain.dto.base.EamAssetTypeDTO;
import com.jcfk.eam.domain.dto.base.EamTeamDTO;
import com.jcfk.eam.domain.dto.base.EamTeamMemberAbilityDTO;
import com.jcfk.eam.domain.dto.base.EamTeamMemberAbilityFileDTO;
import com.jcfk.eam.domain.po.base.EamRepairOrderFile;
import com.jcfk.eam.domain.po.base.EamTeamMemberAbility;
import com.jcfk.eam.domain.po.base.EamTeamMemberAbilityFile;
import com.jcfk.eam.domain.query.base.EamTeamMemberAbilityQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 班组成员能力
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-18
 */
@Service
public class EamTeamMemberAbilityService extends BaseService<EamTeamMemberAbilityDao, EamTeamMemberAbility> {


    @Autowired
    private EamAssetTypeService eamAssetTypeService;

    @Autowired
    private EamTeamService eamTeamService;

    @Autowired
    private EamTeamMemberAbilityFileService eamTeamMemberAbilityFileService;

    /**
     * EamTeamMemberAbility分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamTeamMemberAbilityDTO> queryByPage(DataPage<EamTeamMemberAbilityDTO> page, EamTeamMemberAbilityQuery query) {
        // 分页查询
        query.setMemberName(StringUtils.isEmpty(query.getMemberName()) ? query.getMemberName() : query.getMemberName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamTeamMemberAbility是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamTeamMemberAbilityDTO dto) {
        QueryWrapper<EamTeamMemberAbility> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        return RetResult.ok();
    }

    /**
     * 插入班组成员能力
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamTeamMemberAbilityDTO dto) {

        // 保存
        EamTeamMemberAbility item = new EamTeamMemberAbility();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);
        item.setEnabled(AvailableStatus.ENABLE.getValue());
        item.setId(IdUtils.newId());

        super.save(item);
        eamTeamMemberAbilityFileService.saveBatch(dto.getFiles(), item.getId());
    }

    /**
     * 更新班组成员能力
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamTeamMemberAbilityDTO dto) {

        // 保存
        EamTeamMemberAbility item = new EamTeamMemberAbility();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
        eamTeamMemberAbilityFileService.saveBatch(dto.getFiles(), item.getId());
    }

    /**
     * 根据批量主键删除班组成员能力(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamTeamMemberAbilityDTO dto) {

        EamTeamMemberAbility item = new EamTeamMemberAbility();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamTeamMemberAbility> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamTeamMemberAbility item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取班组成员能力信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamTeamMemberAbilityDTO get(String id) {
        // 查询数据
        EamTeamMemberAbility item = this.baseMapper.selectById(id);
        EamTeamMemberAbilityDTO dto = new EamTeamMemberAbilityDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        EamAssetTypeDTO eamAssetTypeDTO = eamAssetTypeService.get(dto.getAssetTypeId());
        dto.setAssetTypeCode(eamAssetTypeDTO.getAssetTypeCode());
        dto.setAssetTypeName(eamAssetTypeDTO.getAssetTypeName());

        EamTeamDTO eamTeamDTO = eamTeamService.get(dto.getTeamId());
        dto.setTeamName(eamTeamDTO.getTeamName());

        // 附件
        QueryWrapper<EamTeamMemberAbilityFile> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EamTeamMemberAbilityFile::getTeamMemberAbilityId, dto.getId());
        List<EamTeamMemberAbilityFile> fileList = eamTeamMemberAbilityFileService.list(wrapper);
        dto.setFiles(convertList(fileList, EamTeamMemberAbilityFileDTO.class));
        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamTeamMemberAbilityDTO>
     */
    public List<EamTeamMemberAbilityDTO> getList(EamTeamMemberAbilityQuery query) {
        // 查询条件
        QueryWrapper<EamTeamMemberAbility> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getMemberEmpNo()), EamTeamMemberAbility::getMemberEmpNo, query.getMemberEmpNo());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getMemberName()), EamTeamMemberAbility::getMemberName, query.getMemberName());
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetTypeId()), EamTeamMemberAbility::getAssetTypeId, query.getAssetTypeId());
        qw.lambda().eq(query.getSkillProficiency()!=null, EamTeamMemberAbility::getSkillProficiency, query.getSkillProficiency());
        // 查询数据
        List<EamTeamMemberAbility> list = super.list(qw);

        return convertList(list, EamTeamMemberAbilityDTO.class);
    }

    /**
     * 根据主键获取班组成员能力信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamTeamMemberAbility item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

